package xin.ssm.jxc.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;

import xin.ssm.jxc.model.Goods;
import xin.ssm.jxc.services.IGoodsService;
import xin.ssm.jxc.util.Result;

@Controller
@RequestMapping("/goods")
public class GoodsController {
	@Resource
	private IGoodsService goodsService;

	// 查询商品
	@RequestMapping("/findgoodsdynamic")
	@ResponseBody
	public Result findGoodsDynamic(@RequestParam(value = "goodsname") String goodsname,
			@RequestParam(value = "categoryid") String categoryid) {
		if (goodsname.equals("") || goodsname.isEmpty()) {
			goodsname = null;
		}
		if (categoryid.equals("") || categoryid.isEmpty()) {
			categoryid = "0";
		}
		Goods g = new Goods();
		g.setGoodsname(goodsname);
		g.setCategoryid(Integer.parseInt(categoryid));
		List<Goods> goodsList = goodsService.findGoodsDynamic(g);
//		for(Goods gs:goodsList) {
//			System.out.println(gs.getCategory().getCategoryname());
//		}
		// 时间转换
		String jsonarry = JSONArray.toJSONStringWithDateFormat(goodsList, "yyyy-MM-dd");
		Result result = new Result();
		result.setCode(0);
		result.setMsg("成功");
		result.setCount(goodsList.size());
		result.setData(goodsList);
		return result;
	}
	//页面查询准备数据
	@RequestMapping("/findallgoods")
	@ResponseBody
	public List<Goods> findEmployee() {
		List<Goods> list = goodsService.findAllgoods();
		return list;
	}
	// 删除
	@RequestMapping("/deletegoods")
	@ResponseBody
	public String deleteGoods(@RequestParam(value = "goodsid") String goodsid) {
		int ret = goodsService.deleteGoods(Integer.parseInt(goodsid));
		if (ret > 0) {
			return "1";
		} else {
			return "0";
		}
	}

	// 修改商品
	@RequestMapping("/updategoods")
	@ResponseBody
	public String updateGoods(@RequestParam(value = "goodsid") Integer goodsid,
			@RequestParam(value = "goodsname") String goodsname, @RequestParam(value = "goodsunit") String goodsunit,
			@RequestParam(value = "goodsprice") BigDecimal goodsprice,
			@RequestParam(value = "categoryname") Integer categoryname) {// 类别id
		Goods g = new Goods();
		g.setGoodsid(goodsid);
		g.setGoodsname(goodsname);
		g.setGoodsunit(goodsunit);
		g.setGoodsprice(goodsprice);
		g.setCategoryid(categoryname);
		int ret = goodsService.updateGoods(g);
		if (ret > 0) {
			return "1";
		} else {
			return "0";
		}
	}

	// 新增商品
	@RequestMapping("/addgoods")
	@ResponseBody
	public String addGoods(
			@RequestParam(value = "goodsname") String goodsname,
			@RequestParam(value = "goodsunit") String goodsunit,
			@RequestParam(value = "goodsprice") BigDecimal goodsprice,
			@RequestParam(value = "categoryname") Integer categoryname) {// 类别id
		Goods g = new Goods();
		g.setGoodsname(goodsname);
		g.setGoodsunit(goodsunit);
		g.setGoodsprice(goodsprice);
		g.setCategoryid(categoryname);
		int ret = goodsService.addGoods(g);
		if (ret > 0) {
			return "1";
		} else {
			return "0";
		}
	}
}
